<template>
    <van-popup v-model="visible" :style="{ padding: '10px', width: '80vw', height: '80vh' }">
        <van-nav-bar :title="keyName" left-text="关闭" @click-left="visible = false;" right-text="选择"
            @click-right="selectword" />
        <van-row>
            <van-col span="24" style="text-align: center;">

                <van-checkbox-group v-model="identityChk" direction="horizontal" style="justify-content: center;">
                    <van-checkbox shape="square" right-icon="video-o" name="0"
                        style="margin-bottom: 10px">识别公库</van-checkbox>
                    <van-checkbox shape="square" right-icon="video-o" name="1"
                        style="margin-bottom: 10px">识别私库</van-checkbox>
                </van-checkbox-group>
            </van-col>
        </van-row>
        <van-tabs>
            <van-tab title="显示库">
                <com-keyword-edit :prop-name="propName" :key-name="keyName" prop-type="text"
                    ref="keywordedit"></com-keyword-edit>
            </van-tab>
            <van-tab title="选择库">
                <com-keyword-edit2 :prop-name="propName" :isCompare="false" :key-name="keyName"
                    ref="keywordEdit2"></com-keyword-edit2>
            </van-tab>
        </van-tabs>
    </van-popup>
</template>
<script>
import { Toast } from 'vant'
import comKeywordEdit from '../comAllTypeJobs/comKeywordEdit.vue';
import comKeywordEdit2 from '../comAllTypeJobs/comKeywordEdit2.vue';
export default {
    name: 'comJobPositionPopup',
    model: {
        prop: "value",
        event: "change",
    },
    components: {
        comKeywordEdit2,
        comKeywordEdit
    },
    props: {
        propName: String,
        keyId: String,
        keyName: String,
        value: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            isInitConfig: false,
            identityChk: ['0', '1'],
            visible: false,
        }
    },

    watch: {
        identityChk(val, oldval) {

            if (this.isInitConfig) {
                this.isInitConfig = false;
                return;
            }
            var pubVal = '0';
            var privateVal = '1';
            var v = val.find(m => m == pubVal);
            if (v != oldval.find(m => m == pubVal))
                this.configChange(v == pubVal, false);


            v = val.find(m => m == privateVal);
            if (v != oldval.find(m => m == privateVal))
                this.configChange(v == privateVal, true);
        },
        value(val) {
            //console.log(1,val);
            this.visible = val;
        },
        visible(val) {
            //console.log(2,val);
            this.$emit("change", val);
            if (val) {
                //setTimeout(() => {
                this.initData();
                this.$nextTick(() => {

                    //console.log("this.$refs", this.$refs);
                    //console.log("this.$refs['keywordedit']", this.$refs['keywordedit']);
                    //console.log("this.$refs.keywordedit", this.$refs.keywordedit);
                    if (this.$refs.keywordedit)
                        this.$refs.keywordedit.refreshData();

                });
                //}, 1000);
                // this.$nextTick(() => {
                // });
            }
        }
    },
    methods: {
        selectword() {
            var params1 = {
                propName: this.propName,
                keyName: this.keyName,
                ids: this.$refs.keywordEdit2.getCheckedIds()
            };
            this.$api.keyword.keywordUpdateSelect(params1).then(res => {
                if (res.code === 200) {
                    Toast('操作成功！')
                } else {
                    Toast('错误！')
                }
            })
            //this.$emit("select-change", this.$refs.keywordEdit2.getSelectWord());
            this.visible = false;
        },
        initData() {
            //this.$refs.keywordEdit2.getList();
            //alert("");
            var params1 = {
                propName: this.propName,
                keyName: this.keyName
            };
            var that = this;
            this.$api.keyword.keywordConfigGetConfig(params1).then(res => {
                if (res.code === 200) {
                    var ary = new Array();
                    if (res.data.item1)
                        ary.push('0');
                    if (res.data.item2)
                        ary.push('1');
                    that.isInitConfig = true;
                    that.identityChk = ary;
                } else {
                    Toast('错误！')
                }
            })
        },
        configChange(bl, isPrivate) {
            var params1 = {
                propName: this.propName,
                keyName: this.keyName,
                isPrivate: isPrivate
            };
            if (bl) {
                this.$api.keyword.keywordConfigDelete(params1).then(res => {
                    if (res.code === 200 && res.data) {
                        Toast('操作成功！')
                    } else {
                        Toast('系统出错！')
                    }
                })
            } else {
                this.$api.keyword.keywordConfigAdd(params1).then(res => {
                    if (res.code === 200 && res.data) {
                        Toast('操作成功！')
                    } else {
                        Toast('系统出错！')
                    }
                })
            }
        },
    },
    mounted() {
        if (this.value) {
            this.visible = true;
        }
    }
}
</script>
<style scoped></style>